/**  
* Copyright (c) 2009, Noel Lynch All rights reserved. Redistribution and use  
* in source and binary forms, with or without modification, are permitted  
* provided that the following conditions are met: Redistributions of source  
* code must retain the above copyright notice, this list of conditions and the  
* following disclaimer. Redistributions in binary form must reproduce the above  
* copyright notice, this list of conditions and the following disclaimer in the  
* documentation and/or other materials provided with the distribution. Neither  
* the name of Noel Lynch nor the names of contributors may be used to  
* endorse or promote products derived from this software without specific prior  
* written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT  
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A  
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR  
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,  
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;  
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR  
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF  
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  
*/
// created by noellynch
// Jul 16, 2009

package com.sceneworker.app.input;

import java.util.ArrayList;

import com.jme.math.Vector3f;
import com.jme.scene.Spatial;

public class AlignmentHandler {
	
	static	public	void	alignAlongX(Spatial cl_ref, boolean b_initialiseUndo, Spatial ...cla_spatials) {
		if(cl_ref == null) { return; }
		
		if(!b_initialiseUndo) {
			SpatialSelectionHandler.initialiseUndoTranslation(cl_ref, cla_spatials);
		}
		
		Vector3f		lcl_worldLoc = cl_ref.getWorldTranslation();
		float			lf_diffX;
		
		for(Spatial lcl_other : cla_spatials) {
			Vector3f		lcl_otherWorldLoc = lcl_other.getWorldTranslation();
			lf_diffX = lcl_worldLoc.x - lcl_otherWorldLoc.x;
			
			lcl_other.getLocalTranslation().x += lf_diffX;
			lcl_other.updateGeometricState(0, true);
		}
	}
	
	public	void	alignAlongY(Spatial cl_ref, boolean b_initialiseUndo, Spatial ...cla_spatials) {
		if(cl_ref == null) { return; }
		
		if(!b_initialiseUndo) {
			SpatialSelectionHandler.initialiseUndoTranslation(cl_ref, cla_spatials);
		}
		
		Vector3f		lcl_worldLoc = cl_ref.getWorldTranslation();
		float			lf_diffY;
		
		for(Spatial lcl_other : cla_spatials) {
			Vector3f		lcl_otherWorldLoc = lcl_other.getWorldTranslation();
			lf_diffY = lcl_worldLoc.y - lcl_otherWorldLoc.y;
			
			lcl_other.getLocalTranslation().y += lf_diffY;
			lcl_other.updateGeometricState(0, true);
			
		}
	}
	
	public	void	alignAlongZ(Spatial cl_ref, boolean b_initialiseUndo, Spatial ...cla_spatials) {
		if(cl_ref == null) { return; }
		
		if(!b_initialiseUndo) {
			SpatialSelectionHandler.initialiseUndoTranslation(cl_ref, cla_spatials);
		}
		
		Vector3f		lcl_worldLoc = cl_ref.getWorldTranslation();
		float			lf_diffZ;
		
		for(Spatial lcl_other : cla_spatials) {
			Vector3f		lcl_otherWorldLoc = lcl_other.getWorldTranslation();
			lf_diffZ = lcl_worldLoc.z - lcl_otherWorldLoc.z;
			
			lcl_other.getLocalTranslation().z += lf_diffZ;
			lcl_other.updateGeometricState(0, true);
		}
	}
	
	public	void	spaceAlongX(Spatial cl_ref, boolean b_initialiseUndo, Spatial ...cla_spatials) {
		if(cl_ref == null) { return; }
		
		if(cla_spatials.length <= 1) { return; }
		
		if(!b_initialiseUndo) {
			SpatialSelectionHandler.initialiseUndoTranslation(cl_ref, cla_spatials);
		}
		
		// get the last selected spatial
		Spatial		lcl_last = cla_spatials[cla_spatials.length - 1];
		
		Vector3f		lcl_startWorldLoc = cl_ref.getWorldTranslation();
		Vector3f		lcl_endWorldLoc = lcl_last.getWorldTranslation();
		Vector3f		lcl_newWorldLoc = new Vector3f();
		Vector3f		lcl_newLocalLoc = new Vector3f();
		
		float			lf_xDiff;
		
		lf_xDiff = lcl_endWorldLoc.x - lcl_startWorldLoc.x;
		
		float			lf_xDelta = lf_xDiff / cla_spatials.length;
		float			lf_tempDelta = lf_xDelta;
		
		for(int li_index = 0; li_index < (cla_spatials.length - 1); li_index++, lf_tempDelta += lf_xDelta) {
			Spatial		lcl_other = cla_spatials[li_index];
			lcl_newWorldLoc = new Vector3f(lcl_other.getWorldTranslation());
			lcl_newWorldLoc.x = lcl_startWorldLoc.x + lf_tempDelta;
			
			lcl_other.worldToLocal(lcl_newWorldLoc, lcl_newLocalLoc);
			
			lcl_other.getLocalTranslation().x += lcl_newLocalLoc.x;
			lcl_other.updateGeometricState(0, true);
		}
	}
	
	public	void	spaceAlongZ(Spatial cl_ref, boolean b_initialiseUndo, Spatial ...cla_spatials) {
		if(cl_ref == null) { return; }
		
		if(cla_spatials.length <= 1) { return; }
		
		if(!b_initialiseUndo) {
			SpatialSelectionHandler.initialiseUndoTranslation(cl_ref, cla_spatials);
		}
		
		// get the last selected spatial
		Spatial		lcl_last = cla_spatials[cla_spatials.length - 1];
		
		Vector3f		lcl_startWorldLoc = cl_ref.getWorldTranslation();
		Vector3f		lcl_endWorldLoc = lcl_last.getWorldTranslation();
		Vector3f		lcl_newWorldLoc = new Vector3f();
		Vector3f		lcl_newLocalLoc = new Vector3f();
		
		float			lf_zDiff;
		
		lf_zDiff = lcl_endWorldLoc.z - lcl_startWorldLoc.z;
		
		float			lf_zDelta = lf_zDiff / cla_spatials.length;
		float			lf_tempDelta = lf_zDelta;
		
		for(int li_index = 0; li_index < (cla_spatials.length - 1); li_index++, lf_tempDelta += lf_zDelta) {
			Spatial		lcl_other = cla_spatials[li_index];
			lcl_newWorldLoc = new Vector3f(lcl_other.getWorldTranslation());
			lcl_newWorldLoc.z = lcl_startWorldLoc.z + lf_tempDelta;
			
			lcl_other.worldToLocal(lcl_newWorldLoc, lcl_newLocalLoc);
			
			lcl_other.getLocalTranslation().z += lcl_newLocalLoc.z;
			lcl_other.updateGeometricState(0, true);
		}
	}
	
	public	void	spaceAlongY(Spatial cl_ref, boolean b_initialiseUndo, Spatial ...cla_spatials) {
		if(cl_ref == null) { return; }
		
		if(cla_spatials.length <= 1) { return; }
		
		if(!b_initialiseUndo) {
			SpatialSelectionHandler.initialiseUndoTranslation(cl_ref, cla_spatials);
		}
		
		// get the last selected spatial
		Spatial		lcl_last = cla_spatials[cla_spatials.length - 1];
		
		Vector3f		lcl_startWorldLoc = cl_ref.getWorldTranslation();
		Vector3f		lcl_endWorldLoc = lcl_last.getWorldTranslation();
		Vector3f		lcl_newWorldLoc = new Vector3f();
		Vector3f		lcl_newLocalLoc = new Vector3f();
		
		float			lf_yDiff;
		
		lf_yDiff = lcl_endWorldLoc.y - lcl_startWorldLoc.y;
		
		float			lf_yDelta = lf_yDiff / cla_spatials.length;
		float			lf_tempDelta = lf_yDelta;
		
		for(int li_index = 0; li_index < (cla_spatials.length - 1); li_index++, lf_tempDelta += lf_yDelta) {
			Spatial		lcl_other = cla_spatials[li_index];
			lcl_newWorldLoc = new Vector3f(lcl_other.getWorldTranslation());
			lcl_newWorldLoc.y = lcl_startWorldLoc.y + lf_tempDelta;
			
			lcl_other.worldToLocal(lcl_newWorldLoc, lcl_newLocalLoc);
			
			lcl_other.getLocalTranslation().y += lcl_newLocalLoc.y;
			lcl_other.updateGeometricState(0, true);
		}
	}
}
